package cc.rengu.igas.route.core.realize.payment.impl.nucc;

import cc.rengu.igas.channel.nucc.biz.impl.NuccAcqPayServiceImpl;
import cc.rengu.igas.channel.nucc.facade.base.Header;
import cc.rengu.igas.channel.nucc.facade.request.VerifySignRequest;
import cc.rengu.igas.channel.nucc.facade.response.VerifySignResponse;
import cc.rengu.igas.channel.nucc.facade.result.Result;
import cc.rengu.igas.route.common.constant.SmartRouteParamConstant;
import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.core.realize.payment.DstChannelPaymentSignService;
import cc.rengu.igas.route.facade.request.IntelligencePaymentSignRequest;
import cc.rengu.igas.route.facade.request.IntelligenceResultQueryRequest;
import cc.rengu.igas.route.facade.response.IntelligencePaymentSignResponse;
import cc.rengu.igas.route.facade.response.IntelligenceResultQueryResponse;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;

/**
 * 网联对外收款签约实现，协议签约身份认证
 *
 * @author lizhangtao
 * @version 1.0.0
 * @date 2020-04-30
 */
public class NuccPaymentSignServiceImpl implements DstChannelPaymentSignService {
    private final static String TXN_NUM = "T00NP101";

    @Override
    public IntelligencePaymentSignResponse paymentSign(IntelligencePaymentSignRequest intelligencePaymentSignRequest) throws Exception {
        RgLogger rglog = RgLog.getLogger(this.getClass().getSimpleName());
        IntelligencePaymentSignResponse intelligencePaymentSignResponse = new IntelligencePaymentSignResponse();
        intelligencePaymentSignResponse.setHeader(intelligencePaymentSignRequest.getHeader());
        intelligencePaymentSignResponse.setDstChannelId("NUCC");
        VerifySignRequest verifySignRequest = new VerifySignRequest();
        //设置请求值
        verifySignRequest.setInstgId(intelligencePaymentSignRequest.getHeader().getInstId());
        verifySignRequest.setSgnAcctIssrId(intelligencePaymentSignRequest.getPayerInfo().getOpenBankNm());
        verifySignRequest.setSgnAcctTp(intelligencePaymentSignRequest.getPayerInfo().getAcctType());
        verifySignRequest.setSgnAcctId(intelligencePaymentSignRequest.getPayerInfo().getAcctNo());
        verifySignRequest.setSgnAcctNm(intelligencePaymentSignRequest.getPayerInfo().getAcctName());
        verifySignRequest.setIdType(intelligencePaymentSignRequest.getPayerInfo().getCertType());
        verifySignRequest.setIdNo(intelligencePaymentSignRequest.getPayerInfo().getCertId());
        verifySignRequest.setMobNo(intelligencePaymentSignRequest.getPayerInfo().getPhoneNo());
        //网联交易类型，区分签约与签约确认 0201协议签约-身份认证
        verifySignRequest.setTrxCtgy("0201");
//        verifySignRequest.setClbckUrl("");
        //设置请求头
        Header header = new Header();
        BeanUtil.beanCopy(header, intelligencePaymentSignRequest.getHeader());
        header.setTxnNum(TXN_NUM);
        verifySignRequest.setHeader(header);
        //调用通道发起签约
        DubboService dubboService = new DubboServiceImpl();
        Result<VerifySignResponse> verifySignResponseResult = (Result<VerifySignResponse>) dubboService.callDubboInnerMethod(NuccAcqPayServiceImpl.class, "verifySignQuick", verifySignRequest);
        if (!verifySignResponseResult.isSuccess() || null == verifySignResponseResult.getResult()) {
            rglog.error("交易未应答信息");
            throw new BizException(SmartRouteRspEnums.TRANS_TO.getRespCode(), SmartRouteRspEnums.TRANS_TO.getRespDesc());
        }
        intelligencePaymentSignResponse.setRespCode(verifySignResponseResult.getResult().getRespCode());
        intelligencePaymentSignResponse.setRespDesc(verifySignResponseResult.getResult().getRespDesc());
        if (OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(verifySignResponseResult.getResult().getRespCode())) {
//            intelligencePaymentSignResponse.setDstChannelSsn();
            intelligencePaymentSignResponse.setVerifyMode("01");
            //存储下来
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("authMsgId", verifySignResponseResult.getResult().getAuthMsgId());
            String dateStr = verifySignRequest.getHeader().getTransDate();
            String timeStr = verifySignRequest.getHeader().getTransTime();
            String isoDateTime = DateUtil.dateStringFormatConvert(dateStr + timeStr, SmartRouteParamConstant.FORMAT_DATE_TIME14, SmartRouteParamConstant.FORMAT_DATE_TIME_ISO_DATE_TIME);
            jsonObject.put("origTrxDtTm", isoDateTime);
            intelligencePaymentSignResponse.setAdditionInfo(jsonObject.toJSONString());
        }
        return intelligencePaymentSignResponse;
    }

    @Override
    public IntelligenceResultQueryResponse query(IntelligenceResultQueryRequest resultQueryRequest) throws Exception {
        return null;
    }
}
